package hdb.controller;

import hdb.dao.ArtikelDao;
import hdb.om.Artikel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class SaveArtikel extends AbstractController {
	private ArtikelDao artikelDao;

	public void setArtikelDao(ArtikelDao artikelDao) {
		this.artikelDao = artikelDao;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		int id = Integer.parseInt(req.getParameter("id"));
		long ean = Long.parseLong(req.getParameter("barcode"));
		String n = req.getParameter("name");
		String haupt = req.getParameter("hauptkategorie");
		String unter = req.getParameter("unterkategorie");
		String g = req.getParameter("geschaeft");
		int mb = Integer.parseInt(req.getParameter("mbestand"));
		
		if (id == 0) {
			artikelDao.create(ean, n, haupt, unter, g, mb);
		} else {
			artikelDao.modify(id, ean, n, haupt, unter, g, mb);
		}

		return new ModelAndView("home");

	}

}
